//网络接口
module.exports = {

    url:"http://193.112.7.91:8080",
    getToken(){
        var token = "1947504048";
        return token;
    },
    fetch(data){
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && (xhr.status>=200 && xhr.status<400)){
                var response = xhr.responseText;
                if(data.success){
                    data.success(JSON.parse(response))
                }
            }else{
                if(data.success){
                    data.success({"code":404})
                }else{
                    cc.log("网络请求异常："+data.url)
                }
            }
        }
        xhr.open(data.type,this.url+data.url);
        xhr.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
        if(data.data && typeof data.data=="object"){
            var arra = new Array();
            var i=0;
            for(var arr in data.data){
                arra[i] = encodeURIComponent(arr)+"="+encodeURIComponent(data.data[arr]);
                i++;
            }
            arra[i] = encodeURIComponent("token")+"="+encodeURIComponent(this.getToken());
            xhr.send(arra.join("&"))
        }else{
            xhr.send(data.data)
        }


    }
}